package net.yambal.android.air.roulette.manager
{
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.system.Security;
	import flash.system.SecurityPanel;
	
	import spark.components.Image;

	public class CamManager
	{
		private static var _instance:CamManager;
		
		private var _cam:Camera = Camera.getCamera();
		
		public function CamManager()
		{

		}
		
		public static function getInstance():CamManager {
			if(_instance == null){
				_instance = new CamManager();
			}
			return _instance;
		}
		
		// Cam
		private function onStatus(e:StatusEvent):void{
			if(e.code == "Camera.Unmuted"){
				_cam.removeEventListener(StatusEvent.STATUS, onStatus);
				connectCamera();
			}
		}
		
		private var _target_image:Image = null;
		private var _target_video:Video = null;
		public function setImageAndVideo(image:Image, video:Video):void{
			_target_image = image;
			_target_video = video;
			
			if(_cam != null){
				if(_cam.muted){
					Security.showSettings(SecurityPanel.PRIVACY);
					_cam.addEventListener(StatusEvent.STATUS,  onStatus);
				}else{
					connectCamera();
				}
			}else{
				
			}
		}
		
		private function connectCamera():void{
			_target_image.visible = false;
			_target_video.visible = true;
			
			_cam.setMode(40, 40, 30);
			_target_video.attachCamera(_cam);
			
			_target_video.addEventListener(MouseEvent.CLICK, onVideoClick);
		}
		
		private function onVideoClick(e:MouseEvent):void{
			trace("onVideoClick");
		}
		
		
	}
}